<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="40%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="物流公司名称" prop="company">
        <el-input placeholder="请输入物流公司名称" maxlength="30" v-model="dataForm.company"></el-input>
      </el-form-item>
      <el-form-item label="物流公司编码" prop="logistics_code">
        <el-input placeholder="请输入物流公司编码" maxlength="30" v-model="dataForm.logistics_code"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="order_index">
        <el-input-number v-model="dataForm.order_index" :precision="0" :min="1"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      visible: false,
      is_pass: 1,
      dataForm: {
        id: '0',
        company: '',
        logistics_code: '',
        order_index: ''
      },
      dataRule: {
        company: [
          { required: true, message: '物流公司名称不能为空', trigger: 'blur' }
        ],
        logistics_code: [
          { required: true, message: '物流公司编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (id) {
        seviceApi.getLogisticsInfo(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
          }
        })
      }
    },
    // 添加，修改
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.id == 0) {
            delete this.dataForm.id
            seviceApi.saveLogistics(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          } else {
            seviceApi.updateLogistics(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
